using System;
using System.Collections;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

public class MarketEquipButton : MonoBehaviour
{
	public UIWidget holder;

	public UISprite buttonSprite;

	public UILabel title;

	private Action action;

	private void OnClick()
	{
		if (action != null)
		{
			action();
		}
		MarketControllerUI.instance.mainButtons.SetMainButtons();
	}

	private void OnEnable()
	{
		CoroutineRunner.Instance.StartCoroutine(FadeIn());
	}

	private IEnumerator FadeIn()
	{
		holder.alpha = 0f;
		HOTween.To(holder, 0.25f, new TweenParms().Prop("alpha", 1f).UpdateType(UpdateType.TimeScaleIndependentUpdate));
		yield return CoroutineRunner.WaitForSeconds(0.26f);
		holder.alpha = 1f;
	}

	public void SetButton()
	{
		if (!MarketControllerUI.instance.selectedProduct.equippable)
		{
			holder.gameObject.SetActive(false);
		}
		else if (MarketControllerUI.instance.selectedProduct is ProductComsumable && ((ProductComsumable)MarketControllerUI.instance.selectedProduct).Count > 0 && !MarketControllerUI.instance.selectedProduct.Equipped)
		{
			action = Equip;
			holder.gameObject.SetActive(true);
			string text = ScriptLocalization.Get("Market/itemEquip");
			title.text = ((!string.IsNullOrEmpty(text)) ? text : "Нет локализации");
		}
		else if (MarketControllerUI.instance.selectedProduct.Purchased && !MarketControllerUI.instance.selectedProduct.Equipped)
		{
			action = Equip;
			holder.gameObject.SetActive(true);
			string text2 = ScriptLocalization.Get("Market/itemEquip");
			title.text = ((!string.IsNullOrEmpty(text2)) ? text2 : "Нет локализации");
		}
		else if (MarketControllerUI.instance.selectedProduct.Purchased && MarketControllerUI.instance.selectedProduct.Equipped && MarketControllerUI.instance.selectedProduct is ProductWearable)
		{
			action = Unequip;
			holder.gameObject.SetActive(true);
			string text3 = ScriptLocalization.Get("Market/itemUnequip");
			title.text = ((!string.IsNullOrEmpty(text3)) ? text3 : "Нет локализации");
		}
		else
		{
			holder.gameObject.SetActive(false);
		}
	}

	private void Equip()
	{
		if (MarketController.Instance.Equip(MarketControllerUI.instance.selectedProduct))
		{
			MarketControllerUI.instance.productsPanel.ResetEquippedProductButton();
			if (MarketControllerUI.instance.selectedProduct is ProductWeapon)
			{
				MarketControllerUI.instance.previewController.DefaultWeapon = MarketControllerUI.instance.selectedProduct;
			}
		}
	}

	private void Unequip()
	{
		if (MarketController.Instance.UnEquip(MarketControllerUI.instance.selectedProduct))
		{
			MarketControllerUI.instance.productsPanel.ResetEquippedProductButton();
		}
	}
}
